#include"GameField.h"

GameField::GameField(int width, int height, const QString &map, QGraphicsScene *Scene,
                     QWidget *parent, Qt::WFlags flags,int ItemLength): m_width(width), m_height(height),itemLength(ItemLength)
{
    m_ptr2_cells = new Cell**[m_height];
    for(int i=0;i<m_height;++i)
    {
        m_ptr2_cells[i] = new Cell*[m_width];
        for(int j=0; j<m_width;++j)
        {
            char type = map[i * m_width + j].toAscii();
            Cell * cell = new Cell(type, j, i);
            int Z=0;
            switch(type)
            {
            case 'W': Z = -3;
                break;
            case 'B': Z = -1;
                break;
            case 'E': Z = -4;
            }
            cell->setZValue(Z);
            cell->addToScene(Scene);
            m_ptr2_cells[i][j] = cell;
        }
    }
}

int GameField::GetFieldWidth()
{
    return m_width;
}

int GameField::GetFieldHaight()
{
    return m_height;
}

void GameField::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    int pixLength=25;
    for(int i=0;i<m_width;++i)
        for(int j=0;j<m_height;++j)
            painter->drawPixmap(i*pixLength,j*pixLength,pixLength,pixLength,*(m_ptr2_cells[i][j]->GetBitmap()));
}

void GameField::advance()
{

}


QRectF GameField::boundingRect() const
 {
    //это от финта:
     return QRectF(0,0, m_width*itemLength,m_height*itemLength);
 }

